{% load aim_tags %}
{% load objective_tags %}
{% load project_tags %}
{% load work_package_tags %}
{% load humanize %}

<div class="objectives">

{% regroup objective_list by aim.work_package.project as projects %}
{% for project_group in projects %}
  <h3>Project: {{ project_group.grouper }} {% project_status project_group.grouper %}</h3>

  <table class="objective" width="100%">
    <tr>
      <th rowspan='2'>Title</th>
      <th width="5%" rowspan='2'>Target Date</th>
      <th width="10%" rowspan='2'>Status</th>
      <th  colspan='6'>Deliverables</th>
    </tr>
    <tr>
      <th width="5%" >Delivered</th>
      <th width="5%">Expected to date</th>
      <th width="5%">Scheduled</th>
      <th width="5%">Expected</th>
      <th width="5%">Overdue</th>
      <th width="25%">Next</th>
      <td/>
    </tr>

    {% for work_package in project_group.grouper.workpackage_set.all %}
      <tr>
        <th colspan="9">Work Package: {{ work_package }}  {% work_package_status work_package %}</th>
      </tr>
      {% for aim in work_package.aim_set.all %}
        <tr><th>Aim: {{ aim }} {% aim_status aim %}</th></tr>
        {% for objective in aim.objective_set.all %}
          <tr>
            <td><a href="{% url objective_detail object_id=objective.id %}">{{ objective.title }}</a></td>
            <td>{{ objective.target_date }}</td>
            <td>{% objective_status objective %}</td>

            <td>{{ objective.delivered }}</td>
            <td>{{ objective.expected_deliverables_to_date|naturalday }}</td>
            <td>{{ objective.deliverable_set.all.count }}</td>

            <td>{{objective.quantity}}</td>

            {% if objective.overdueDeliverables %}
              <td class="warning">{{ objective.overdueDeliverables.count }}</td>
            {% else %}
              <td>{{ objective.overdueDeliverables.count }}</td>
            {% endif %}
      
            {% if objective.nextDeliverable %}
              <td><a href="{% url deliverable_detail object_id=objective.nextDeliverable.id %}">{{ objective.nextDeliverable.title }}</a> (due {{ objective.nextDeliverable.target_date }})</td>
            {% else %}
              <td>N/A</td>
            {% endif %}
            <td><a href="{% url objective_edit objective_id=objective.id %}">Edit</td>
          </tr>
        {% endfor %}
      {% endfor %}
    {% endfor %}

  </table>
{% endfor %}

</div>

